package edu.fiu.cis.selfhealingdisk;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;

public class DiskSimOutputParser {

	private final String outputFileURL;

	public DiskSimOutputParser(String outputFileURL) {

		this.outputFileURL = outputFileURL;

	}

	public Map<String, String> parseStatistics() {

		File file = new File(outputFileURL);

		Map<String, String> statistics = new HashMap<String, String>();

		try {

			Scanner fileScanner = new Scanner(file);

			fileScanner.useDelimiter(System.getProperty("line.separator"));

			while (fileScanner.hasNextLine()) {

				String line = fileScanner.nextLine();

				if (line.contains("Disk Total Requests handled")) {
					statistics.put("Total Requests Handled", line.substring(
							line.indexOf(":") + 1, line.length()).trim());
				}

				else if (line.contains("Disk Completely idle time")) {
					String[] values = line.substring(line.indexOf(":") + 1,
							line.length()).trim().split("\t");
					statistics.put("Completely Iddle Time", values[0]);
					statistics.put("Completely Iddle Time Percentage",
							values[1]);
				}

				if (line.contains("Disk Number of reads")) {
					String[] values = line.substring(line.indexOf(":") + 1,
							line.length()).trim().split("\t");
					statistics.put("Reads", values[0]);
					statistics.put("Reads Percentage", values[1]);
				}

				if (line.contains("Disk Number of writes")) {
					String[] values = line.substring(line.indexOf(":") + 1,
							line.length()).trim().split("\t");
					statistics.put("Writes", values[0]);
					statistics.put("Writes Percentage", values[1]);
				}

				if (line.contains("Disk Sequential reads")) {
					String[] values = line.substring(line.indexOf(":") + 1,
							line.length()).trim().split("\t");
					statistics.put("Sequential Reads", values[0]);
					statistics.put("Sequential Reads Percentage", values[1]);
				}

				if (line.contains("Disk Sequential writes")) {
					String[] values = line.substring(line.indexOf(":") + 1,
							line.length()).trim().split("\t");
					statistics.put("Sequential Writes", values[0]);
					statistics.put("Sequential Writes Percentage", values[1]);
				}

				else if (line.contains("Disk simtime")) {
					statistics.put("Simulation Time", line.substring(
							line.indexOf(":") + 1, line.length()).trim());
				}

				else if (line.contains("Disk Seek time average")) {
					statistics.put("Average Seek Time", line.substring(
							line.indexOf(":") + 1, line.length()).trim());
				}

				else if (line
						.contains("Overall I/O System Inter-arrival time average")) {
					statistics.put("Inter-arrival Time Average", line
							.substring(line.indexOf(":") + 1, line.length())
							.trim());
				}

				else if (line
						.contains("Overall I/O System Inter-arrival time std.dev")) {
					statistics.put("Inter-arrival Time Standard Deviation",
							line
									.substring(line.indexOf(":") + 1,
											line.length()).trim());
				} else if (line
						.contains("Overall I/O System Maximum queue length")) {
					statistics.put("Maximum queue length",
							line
									.substring(line.indexOf(":") + 1,
											line.length()).trim());
				}else if (line
						.contains("Overall I/O System Queue time average")) {
					statistics.put("System Queue time average",
							line
									.substring(line.indexOf(":") + 1,
											line.length()).trim());
				}
				
			}

			fileScanner.close();

		} catch (FileNotFoundException e) {
			System.out.println("Could not locate DiskSim's output file");
			System.exit(1);
		}

		return statistics;

	}

	public static void main(String args[]) {

		DiskSimOutputParser parser = new DiskSimOutputParser(
				"/home/barbara/workspace/self-healing-disk/disksim/valid/self_healing_disk.outv");

		Map<String, String> stats = parser.parseStatistics();

		Collection<String> statNames = stats.keySet();

		for (Iterator iter = statNames.iterator(); iter.hasNext();) {
			String next = (String) iter.next();
			System.out.println(next + " = " + stats.get(next));
		}
	}
}
